<!doctype html>
<html style="height: 100%;">
<head>
    <meta charset="utf-8" />
    <title>众筹编辑知识库</title>
    <script type="text/javascript" src="editMd-marked.min.js"></script>
    <script type="text/javascript" src="editMd-purify.min.js"></script>
    <script type="text/javascript" src="editMd-net.js"></script>
</head>
<body style="height: calc(100% - 100px);" onload="onload()">
  <div>
      <div><span style="padding-right: 10px;">你当前编辑的是<a id="fileName"></a></span><button onclick="onCommit()">提交</button></div>
    <div>请注意：你可以随意编辑本页面，但是当前尚未具备在线审查技术，因此采用离线审查，需要等一段时间人工审查后才能合并到主分支</div>
  </div>
  <div style="display: flex;height: calc(100% - 0px);">
    <textarea onchange="updateMd()" onpropertychange="updateMd()" id="md" style="width: 49%;height: 100%;"></textarea>
    <div id="content" style="width: 49%;height: 100%;overflow: scroll;padding:0px 15px"></div>
  </div>
  
  <script>
      let currentFileName = "";
      function onload() {
          //下载md文档
          addHandler("downladMarkdownFile", function (data) {
              document.getElementById('md').innerHTML = data;
              updateMd();
          })
          let mdFileName = location.search.substr(location.search.indexOf('desktopDoc'));
          mdFileName = mdFileName.substr(0, mdFileName.length - 4) + "md";

          currentFileName = decodeURIComponent(mdFileName);
          sendData("downladMarkdownFile:" + currentFileName)
          document.getElementById('fileName').innerText = currentFileName;
      }
      function onCommit() {
          sendData("saveMarkdownFile:" + currentFileName + ";" + document.getElementById('md').value);
      }
      
      document.getElementById('md').addEventListener('input',function(){
        updateMd();
      })
      function updateMd(){
        var v=marked.parse(document.getElementById('md').value);
        var v2=DOMPurify.sanitize(v, { USE_PROFILES: { html: true } });
        document.getElementById('content').innerHTML =v2;
      }
      updateMd();
  </script>
</body>
</html>